package in.muton.edushare.pages.cp.backup;

import in.muton.edushare.components.whiteMenu.WhiteMenu;
import in.muton.edushare.pages.cp.CPBase;
import in.muton.edushare.pages.cp.backup.restore.RestorePanel;

import java.util.ArrayList;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.panel.EmptyPanel;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;

import com.edushare.common.vo.Universities;

public class BackupBase extends CPBase {

	private static Log log = LogFactory.getLog(BackupBase.class);
	public static final ArrayList<String> menuList = new ArrayList<String>();
	private static Model<ArrayList<String>> menuModel;
	static{
		menuList.add("Create Backup");
		menuList.add("Restore from Backup");
		menuModel = new Model<ArrayList<String>>(menuList);
	}
	private Panel selectedMenuPanel = new EmptyPanel("selectedMenuPanel");
	private WhiteMenu menu;
	
	public BackupBase(PageParameters params) {
		super(params);
		loadPage();
	}
	
	public BackupBase(IModel<Universities> univModel) {
		super(univModel, 1);
		loadPage();
	}
	
	private void loadPage(){
		this.menu = new WhiteMenu("menu", menuModel, 0){

			private static final long serialVersionUID = 9027601530963450332L;

			@Override
			protected void onMenuClick(ListItem<String> menuItem) {
				if(menuItem.getModelObject().equals(menuList.get(0)))
					loadPanel(0);
				else if(menuItem.getModelObject().equals(menuList.get(1)))
					loadPanel(1);
				else
					loadPanel(0);
			}
			
		};
		
		add(menu);
		add(selectedMenuPanel);
		loadPanel(0);
	}
	
	private void loadPanel(int menuIndex){
		switch (menuIndex) {
		case 0:
			loadCreateBackupPanel();
			break;

		case 1:
			loadRestoreBackupPanel();
			break;

		default:
			loadCreateBackupPanel();
			break;
		}
	}

	private void loadRestoreBackupPanel() {
		Panel p = new RestorePanel("selectedMenuPanel", univModel, this);
		replacePanel(p);
	}

	private void loadCreateBackupPanel() {
		Panel p = new CreateBackupPanel("selectedMenuPanel", univModel, this);
		replacePanel(p);
	}

	private void replacePanel(Panel p) {
		selectedMenuPanel.replaceWith(p);
		selectedMenuPanel = p;
	}

}
